package com.zone.vehicle.sdk.module.superapp.vehicle.utils;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @Author: pkqlqx
 * @CreateDate: 2022/1/13 10:29
 * @Description:
 */
public class NetUtil {
    private static final String TAG = NetUtil.class.getSimpleName();

    public static final String NETWORK_TYPE_KEY = "NETWORK_TYPE";

    public static final int NETWORK_UN_KNOW = -2;
    /**
     * 没有连接网络
     */
    public static final int NETWORK_NONE = -1;
    /**
     * 移动网络
     */
    public static final int NETWORK_MOBILE = 0;
    /**
     * 无线网络
     */
    public static final int NETWORK_WIFI = 1;

    public static final String SUPER_APP_NET_WORK_CHANGED = "super_app_net_work_changed";

    public static boolean inspectNet(Context context) {
        return isNetConnect(NetUtil.getNetWorkState(context));
    }

    /**
     * 判断有无网络 。
     *
     * @return true 有网, false 没有网络.
     */
    public static boolean isNetConnect(int netMobile) {
        if (netMobile == NETWORK_WIFI) {
            return true;
        } else if (netMobile == NETWORK_MOBILE) {
            return true;
        } else if (netMobile == NETWORK_NONE) {
            return false;
        }
        return false;
    }

    public static int getNetWorkState(Context context) {
        // 得到连接管理器对象
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {

            if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_WIFI)) {
//                if (!ping(context, "diagnose.z-onesoftware.com")){
//                    sendBroadcast(context, NETWORK_NONE);
//                    return NETWORK_NONE;
//                }
                sendBroadcast(context, NETWORK_WIFI);
                return NETWORK_WIFI;
            } else if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_MOBILE)) {
                sendBroadcast(context, NETWORK_MOBILE);
                return NETWORK_MOBILE;
            }
        } else {
            sendBroadcast(context, NETWORK_NONE);
            return NETWORK_NONE;
        }
        sendBroadcast(context, NETWORK_NONE);
        return NETWORK_NONE;
    }

    private static void sendBroadcast(Context context, int type){
        try {
            Intent intent = new Intent();
            intent.setAction(SUPER_APP_NET_WORK_CHANGED);
            intent.putExtra(NETWORK_TYPE_KEY, type);
            context.sendBroadcast(intent);
        }catch (Exception e){
            Log.e(TAG, e.toString());
        }
    }

    public static boolean ping(Context context, String str) {
        Process p;
        try {
            //ping -c 3 -w 100  中  ，-c 是指ping的次数 3是指ping 3次 ，-w 100  以秒为单位指定超时间隔，是指超时时间为100秒
//            p = Runtime.getRuntime().exec("ping -c 3 -w 100 " + str);
            p = Runtime.getRuntime().exec("ping -c 1 -w 5 " + str);
            int status = p.waitFor();

            InputStream input = p.getInputStream();
            BufferedReader in = new BufferedReader(new InputStreamReader(input));
            StringBuffer buffer = new StringBuffer();
            String line = "";
            while ((line = in.readLine()) != null){
                buffer.append(line);
            }
            if (status != 0 || buffer.toString() == null || "".equals(buffer.toString().trim())){
                System.out.println("ping status="+status+ " buffer=" + buffer.toString());
                sendBroadcast(context, NETWORK_NONE);
            }
            return (status == 0);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return false;
    }
}
